package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.config.Constant;
import com.xishu.entity.activity.Activity;
import com.xishu.entity.comment.Comment;
import com.xishu.entity.customer.Customer;
import com.xishu.entity.shop.Praise;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.CustomerService;
import com.xishu.service.LockService;
import com.xishu.service.PraiseService;
import com.xishu.util.ServletUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PraiseController {

    @Autowired
    PraiseService praiseService;

    private LockService localLockService = LockService.getInstance();

    private CommonService commonService = CommonService.getInstance();

    private CustomerService customerService = CustomerService.getInstance();

    @ApiModelProperty(value = "点赞")
    @PostMapping(value = ("/praise/add"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(mustLogin = false, requireCustomerLogin = true)
    public ResponseData add(@RequestBody Praise praise) throws Exception {
        // "点赞类型 0-活动点赞 1-评论点赞"
        VerifyUtil.isEmpty(praise.getType(), praise.getShopId(), praise.getTypeId());
        Long customerId = ServletUtil.getSessionAttribute(Constant.SESSION_CUSTOMER);
        Customer customer = customerService.findCustomer(customerId);
        praise.setHeadImg(customer.getHeadImg());
        praise.setCustomName(customer.getName());
        praise.setCustomerId(customerId);
        // 查询是否有点赞记录
        Praise searchOne = commonService.searchOne(praise);
        if (searchOne == null) {
            Integer type = praise.getType();
            if (type == 0) {
                Object lock = localLockService.getLock(customerId + ":" + praise.getTypeId());
                synchronized (lock) {
                    Activity activity = new Activity();
                    activity.setId(praise.getTypeId());
                    Activity ac = commonService.searchOne(activity);
                    ac.setLikeCount(ac.getLikeCount() + 1);
                    praiseService.savePraiseAndUpdateActivity(praise, ac);
                }
            } else {
                Object lock = localLockService.getLock(customerId + ":" + praise.getTypeId());
                synchronized (lock) {
                    Comment comment = new Comment();
                    comment.setId(praise.getTypeId());
                    Comment co = commonService.searchOne(comment);
                    co.setLikeCount(co.getLikeCount() + 1);
                    praiseService.savePraiseAndUpdateComment(praise, co);
                }
            }

        }

        return ResponseData.emptyResponse();
    }


    @ApiModelProperty(value = "查询点赞")
    @PutMapping(value = ("/praise/search"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody Praise praise) throws Exception {
        VerifyUtil.isEmpty(praise.getShopId(), praise.getTypeId(), praise.getType());
        return commonService.searchResponse(praise);
    }

}
